﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using service;
using model;
using System.IO;

namespace BHTT.Baiviet
{
    public partial class Themsanpham : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            drlNhomsanpham.DataSource = new NhomSanphamTrathatService().getListAllNhomsanpham();
            drlNhomsanpham.DataBind();
        }

        // Chỉ định loại file
        private bool checkFileType(string FileName)
        {
            string ext = Path.GetExtension(FileName);

            if (ext.Equals(".png") || ext.Equals(".jpg") || ext.Equals(".jpeg") || ext.Equals(".gif"))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        // Chỉ định kích thước file
        private bool checkFileSize(HttpPostedFile pf)
        {
            if (pf.ContentLength < 65536)
                return true;
            else
            {
                return false;
            }
        }

        // Chỉ định kích thước ảnh ( nhỏ hơn 100px x 100px )
        private bool checkImageSize()
        {
            using (System.Drawing.Image myImage = System.Drawing.Image.FromStream(FileUpload1.PostedFile.InputStream))
            {
                if (myImage.Height <= 150 && myImage.Width <= 150)
                    return true;
                else
                    return false;
            }

        }

        protected void btnLuu_Click(object sender, EventArgs e)
        {
            SanphamTrathatModel sp = new SanphamTrathatModel();
            if(txtID.Text != "")
                sp.ID = Convert.ToInt64(txtID.Text);
            sp.IDLoaisanpham = Convert.ToInt64(drlLoaisanpham.SelectedValue);
            sp.Masanpham = txtMasanpham.Text;
            sp.Tensanpham = txtTensanpham.Text;
            sp.Soluong = Convert.ToInt64(txtSoluong.Text);
            sp.Gia = Convert.ToDouble(txtGiaban.Text);
            sp.Mota = txtMota.Content;
            FileUpload1.SaveAs(Server.MapPath("~/Image/") + FileUpload1.FileName);
            sp.Anhdaidien = "../Image/" + FileUpload1.FileName;
            if (rdbSua.Checked)
            {
                new SanphamTrathatService().suaSanpham(sp);
            }
            else
            {
                new SanphamTrathatService().themmoiSanpham(sp);
            }
        }

        protected void drlNhomsanpham_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<LoaiSanphamTrathat> lst = new List<LoaiSanphamTrathat>();
            lst = new LoaisanphamService().getListLoaiSanphamByNhom(Convert.ToInt64(drlNhomsanpham.SelectedValue));
            if (lst.Count > 0)
            {
                drlLoaisanpham.DataSource = lst;
                drlLoaisanpham.DataBind();
            }
        }
    }
}